unit GMedPirkPIrasai;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GFixForm, bendri, Sandelys_dm, StdCtrls, Mask,
  DBCtrls, Buttons, ActnList,
  ExtCtrls, cxLookAndFeelPainters, cxDropDownEdit, cxCalc,
  cxDBEdit, cxMaskEdit, cxLookupEdit, cxDBLookupEdit, cxDBLookupComboBox,
  cxButtons, cxControls, cxContainer, cxEdit, cxTextEdit, DB,
  cxPropertiesStore, Menus, cxGraphics;

type
  TfMedPirkPIrasai = class(TGamaFixForm)
    sMode: TStaticText;
    ActionList1: TActionList;
    aPaimtiPreke: TAction;
    pOK: TPanel;
    DS: TDataSource;
    sbPaimtiSandeli: TcxButton;
    ePrekesID: TcxDBTextEdit;
    eSandelis: TcxDBLookupComboBox;
    sbPaimtiPrekeKodas: TcxButton;
    eKodas: TcxDBLookupComboBox;
    sbPaimtiPrekePavadinimas: TcxButton;
    ePavadinimas: TcxDBLookupComboBox;
    Label5: TLabel;
    eKiekis: TcxDBCalcEdit;
    Label4: TLabel;
    eValKaina: TcxDBCalcEdit;
    Label7: TLabel;
    bOK: TcxButton;
    bCancel: TcxButton;
    GamaPropertiesStore: TcxPropertiesStore;
    Label1: TLabel;
    dbeMatas: TcxDBTextEdit;
    bPadalinys: TcxButton;
    dbePadalinysKodas: TcxDBLookupComboBox;
    dbePadalinysPavadinimas: TcxDBLookupComboBox;
    bPadalinysValyti: TcxButton;
    bFSValyti: TcxButton;
    dbeFSPavadinimas: TcxDBLookupComboBox;
    dbeFSKodas: TcxDBLookupComboBox;
    bFS: TcxButton;
    Label29: TLabel;
    Label2: TLabel;
    ePNr: TcxDBTextEdit;
    Label3: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    eStoris: TcxDBLookupComboBox;
    Label9: TLabel;
    ePlotis: TcxDBLookupComboBox;
    eIlgis: TcxDBLookupComboBox;
    eRusis: TcxDBLookupComboBox;
    Label10: TLabel;
    eSuma: TcxTextEdit;
    ecIlgis: TcxDBCalcEdit;
    Label11: TLabel;
    ePakNr: TcxDBTextEdit;
    eTipas: TcxDBLookupComboBox;
    eVTuris: TcxCalcEdit;
    procedure FormShow(Sender: TObject);
    procedure eValKainaChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure aPaimtiPrekeExecute(Sender: TObject);
    procedure pBSkaneriaiEnter(Sender: TObject);
    procedure pBSkaneriaiExit(Sender: TObject);
    procedure sbPaimtiSandeliClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure bOKClick(Sender: TObject);
    procedure eKiekisPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
    procedure eValKainaPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
    procedure bPadalinysClick(Sender: TObject);
    procedure bFSClick(Sender: TObject);
    procedure bPadalinysValytiClick(Sender: TObject);
    procedure bFSValytiClick(Sender: TObject);
  private
    { Private declarations }
    iSandelis: integer;
    iPrekesKodas : integer;
    PVM: double;
    procedure SetMode(v: string);
    procedure PriimtiPreke(Sender: TObject);
    procedure PriimtiSandeli(Sender: TObject);
    procedure PriimtiPadalinys(Sender: TObject);
    procedure PriimtiFS(Sender: TObject);

    procedure ePrekesIDChange(Sender: TField); // nustatome matavimo vnt.
    procedure eTipasChange(Sender: TField);
    procedure eIlgisChange(Sender: TField);
  public
    { Public declarations }
    procedure ClearMode;
    property Mode: string write SetMode;
  end;

var
  fMedPirkPIrasai: TfMedPirkPIrasai;

implementation

uses
  Math, sandelys_conf, doktip, uSandDokTip, med_dm,
  tools, JvJCLUtils;

{$R *.dfm}

procedure TfMedPirkPIrasai.FormShow(Sender: TObject);
begin
  DS.Enabled := true;

  //
  eValKaina.Properties.DisplayFormat :=
    '0.'+ MakeStr('#', dmSandelys.SandConfigParam.KainosTikslumas);
  eKiekis.Properties.DisplayFormat :=
    '0.'+ MakeStr('#', dmSandelys.SandConfigParam.KainosTikslumas);
  //
  eValKaina.Properties.OnEditValueChanged := eValKainaChange;
  eVTuris.Properties.OnEditValueChanged := eValKainaChange;

  DS.DataSet.FieldByName('PrekesID').OnChange := ePrekesIDChange;
  DS.DataSet.FieldByName('tipas').OnChange := eTipasChange;

  DS.DataSet.FieldByName('storis').OnChange := eIlgisChange;
  DS.DataSet.FieldByName('plotis').OnChange := eIlgisChange;
  DS.DataSet.FieldByName('ilgis').OnChange := eIlgisChange;
  DS.DataSet.FieldByName('kiekis').OnChange := eIlgisChange;
  //
  eValKainaChange(nil);
  eTipasChange(nil);
  eIlgisChange(nil);
  //
  if (DS.DataSet.State in [dsInsert, dsEdit]) and (iSandelis <> -1) then begin
    DS.DataSet.FieldByName('Sandelys').AsInteger := iSandelis;
    ActiveControl := eKodas
  end else
    ActiveControl := eSandelis;
  //
  if (DS.DataSet.State in [dsInsert, dsEdit]) and (iPrekesKodas <> -1) then begin
    DS.DataSet.FieldByName('PrekesID').AsInteger := iPrekesKodas;
    ActiveControl := ePNr;
  end else
    ActiveControl := eKodas;


end;

procedure TfMedPirkPIrasai.eValKainaChange(Sender: TObject);
var
  fSuma: Extended;
begin
  fSuma := eValKaina.Value * eVTuris.Value;
  eSuma.Text := FormatFloat( dmSandelys.SandConfigParam.KainaDisplay, fSuma ) + ' ';
end;

procedure TfMedPirkPIrasai.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  DS.Enabled := false;

  ePrekesID.Properties.OnEditValueChanged := nil;
  eValKaina.Properties.OnEditValueChanged := nil;
  eKiekis.Properties.OnEditValueChanged := nil;

  DS.DataSet.FieldByName('PrekesID').OnChange := nil;
  DS.DataSet.FieldByName('tipas').OnChange := nil;

  DS.DataSet.FieldByName('storis').OnChange := nil;
  DS.DataSet.FieldByName('plotis').OnChange := nil;
  DS.DataSet.FieldByName('ilgis').OnChange := nil;
  DS.DataSet.FieldByName('kiekis').OnChange := nil;
end;

procedure TfMedPirkPIrasai.SetMode(v: string);
begin
  sMode.Caption := v;
end;

procedure TfMedPirkPIrasai.ClearMode;
begin
  sMode.Caption := '';
end;

procedure TfMedPirkPIrasai.aPaimtiPrekeExecute(Sender: TObject);
var
  V: Variant;
begin
  V := VarArrayOf([Unassigned, Unassigned, Unassigned, Unassigned]);
  V[EV_PP_Preke] := DS.DataSet.FieldByName('PrekesID').AsInteger;
  V[EV_PP_Sandelis] := DS.DataSet.FieldByName('Sandelys').AsInteger;
  V[EV_PP_SN] := '';

  RegDokTipas[dtPrekes].Forma.ExValue := V;
  RegDokTipas[dtPrekes].Forma.Paimti(self.PriimtiPreke);
end;

procedure TfMedPirkPIrasai.PriimtiPreke(Sender: TObject);
begin
  with dmSandelys do begin
      if not (DS.DataSet.State in [dsInsert, dsEdit]) then Exit;
      RegDokTipas[dtPrekes].Forma.RefreshTables;
      DS.DataSet.FieldByName('ValKaina').Clear;
      DS.DataSet['PrekesID'] := tbPrekes['ID'];
      DS.DataSet['Kiekis']   := 1;

    if RegDokTipas[dtPrekes].Forma.ExValue[EV_PP_Sandelis] <> -1 then
        DS.DataSet['Sandelys'] := RegDokTipas[dtPrekes].Forma.ExValue[EV_PP_Sandelis];

  end; // with dmSandelys do begin
  RegDokTipas[dtPrekes].Forma.Paimti(nil);
  ActiveControl := eKiekis;
end;


procedure TfMedPirkPIrasai.pBSkaneriaiEnter(Sender: TObject);
begin
  KeyPreview := false;
end;

procedure TfMedPirkPIrasai.pBSkaneriaiExit(Sender: TObject);
begin
  KeyPreview := true;
end;

procedure TfMedPirkPIrasai.sbPaimtiSandeliClick(Sender: TObject);
begin
  RegDokTipas[dtSandeliai].Forma.Paimti(self.PriimtiSandeli);
end;

procedure TfMedPirkPIrasai.PriimtiSandeli(Sender: TObject);
begin
  with dmSandelys do begin
      if not (DS.DataSet.State in [dsInsert, dsEdit]) then Exit;
      RegDokTipas[dtSandeliai].Forma.RefreshTables;
      DS.DataSet['Sandelys'] := RegDokTipas[dtSandeliai].Forma.ExValue;
  end; // with dmSandelys do begin
  RegDokTipas[dtSandeliai].Forma.Paimti(nil);
  ActiveControl := eKodas;
end;

procedure TfMedPirkPIrasai.FormCreate(Sender: TObject);
begin
  DS.Enabled := false;
  iSandelis := -1;
  iPrekesKodas := -1;

  eSandelis.Width := self.ClientWidth - eSandelis.Left - 20;
  ePavadinimas.Width := self.ClientWidth - ePavadinimas.Left - 20;
  //btPaimtiDet.Left := self.ClientWidth - btPaimtiDet.Width - 20;
  //eKodas.Width := btPaimtiDet.Left - 8 - eKodas.Left;

  bPadalinysValyti.Left := self.ClientWidth - bPadalinysValyti.Width - 20;
  dbePadalinysPavadinimas.Width := bPadalinysValyti.Left - 8 - dbePadalinysPavadinimas.Left;

  bFSValyti.Left := self.ClientWidth - bFSValyti.Width - 20;
  dbeFSPavadinimas.Width := bFSValyti.Left - 8 - dbeFSPavadinimas.Left;
  
end;

procedure TfMedPirkPIrasai.bOKClick(Sender: TObject);
begin
  if not (DS.DataSet.Eof and DS.DataSet.Bof) then begin
    iSandelis := DS.DataSet.FieldByName('Sandelys').AsInteger;
    iPrekesKodas := DS.DataSet.FieldByName('PrekesID').AsInteger;
  end;
  ModalResult := mrOK;
end;

procedure TfMedPirkPIrasai.eKiekisPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  if DisplayValue <> '' then
  DisplayValue := Rounding(DisplayValue, dmSandelys.SandConfigParam.KiekioTikslumas);
end;

procedure TfMedPirkPIrasai.eValKainaPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  if DisplayValue <> '' then
  DisplayValue := Rounding(DisplayValue, dmSandelys.SandConfigParam.KainosTikslumas);
end;

procedure TfMedPirkPIrasai.ePrekesIDChange(Sender: TField);
var
  Matas: string;
begin
  // nustatome Matavimo vnt
  if dmSandelys.tbPrekesLookup.Locate('ID',
         DS.DataSet.FieldByName('PrekesID').AsInteger, []) then begin
    Matas := dmSandelys.tbPrekesLookup.FieldByName('Matas').AsString;
  end else
    Matas := '';

  if DS.DataSet.State in [dsEdit, dsInsert] then begin
    DS.DataSet['Matas'] := Matas
  end; // if DS.DataSet.State
end;

procedure TfMedPirkPIrasai.eTipasChange(Sender: TField);
begin
  if (DS.DataSet.FieldByName('Tipas').AsString = 'P') or
      (DS.DataSet.FieldByName('Tipas').AsString = 'G') then begin
    eStoris.RepositoryItem := dmMed.cxEditRepositoryLookupParkStoris;
    ePlotis.RepositoryItem := dmMed.cxEditRepositoryLookupParkPlotis;
    eIlgis.RepositoryItem := nil;
    eRusis.RepositoryItem := dmMed.cxEditRepositoryLookupParkRusis;

    eIlgis.Visible := false;
    ePakNr.Enabled := true;
    ecIlgis.Visible := true;

    ePlotis.Enabled := true;
    eKiekis.Enabled := true;
  end
  else if (DS.DataSet.FieldByName('Tipas').AsString = 'M') then begin
    eStoris.RepositoryItem := dmMed.cxEditRepositoryLookupMedStoris;
    ePlotis.RepositoryItem := dmMed.cxEditRepositoryLookupMedPlotis;
    eIlgis.RepositoryItem := dmMed.cxEditRepositoryLookupMedIlgis;
    eRusis.RepositoryItem := dmMed.cxEditRepositoryLookupMedRusis;

    ePakNr.Enabled := false;
    eIlgis.Visible := true;
    ecIlgis.Visible := false;

    ePlotis.Enabled := true;
    eKiekis.Enabled := true;
  end
  else if (DS.DataSet.FieldByName('Tipas').AsString = 'R') then begin
    eStoris.RepositoryItem := dmMed.cxEditRepositoryLookupRastStoris;
    ePlotis.RepositoryItem := nil;
    eIlgis.RepositoryItem := dmMed.cxEditRepositoryLookupRastIlgis;
    eRusis.RepositoryItem := dmMed.cxEditRepositoryLookupMedRusis;

    ePlotis.Enabled := false;
    eKiekis.Enabled := false;

    ePakNr.Enabled := false;
    eIlgis.Visible := true;
    ecIlgis.Visible := false;

    DS.DataSet['plotis'] := 0.0;
    DS.DataSet['kiekis'] := 1;
  end;
end;

procedure TfMedPirkPIrasai.eIlgisChange(Sender: TField);
var
  storis: Double;
  plotis: Double;
  ilgis: Double;
  kiekis: Double;
  turis: Double;
  plotas: Double;
begin
  if (DS.DataSet.FieldByName('Tipas').AsString = 'R') then begin //Rastai
    storis := DS.DataSet.FieldByName('storis').AsFloat;
    ilgis := DS.DataSet.FieldByName('ilgis').AsFloat;
    kiekis := DS.DataSet.FieldByName('kiekis').AsFloat;

    if not dmMed.tbMedRastKub.Active then dmMed.tbMedRastKub.Open;
    turis := VarToFloat(dmMed.tbMedRastKub.Lookup('galas;ilgis', VarArrayOf([storis, ilgis]), 'turis'));

    DS.DataSet['turis'] := turis;

    eVTuris.Value := turis*kiekis;

  end;
  if (DS.DataSet.FieldByName('Tipas').AsString = 'M') then begin //Mediena
    storis := DS.DataSet.FieldByName('storis').AsFloat;
    plotis := DS.DataSet.FieldByName('plotis').AsFloat;
    ilgis := DS.DataSet.FieldByName('ilgis').AsFloat;
    kiekis := DS.DataSet.FieldByName('kiekis').AsFloat;

    turis := storis*plotis*ilgis;

    DS.DataSet['turis'] := turis;

    eVTuris.Value := turis*kiekis;
  end;
  if (DS.DataSet.FieldByName('Tipas').AsString = 'P') then begin //Parketas
    storis := DS.DataSet.FieldByName('storis').AsFloat;
    plotis := DS.DataSet.FieldByName('plotis').AsFloat;
    ilgis := DS.DataSet.FieldByName('ilgis').AsFloat;
    kiekis := DS.DataSet.FieldByName('kiekis').AsFloat;

    turis := storis*plotis*ilgis/1000000000;
    plotas := plotis*ilgis/1000000.0;

    DS.DataSet['turis'] := turis;

    eVTuris.Value := turis*kiekis;

    DS.DataSet['plotas'] := plotas;
  end;
  if (DS.DataSet.FieldByName('Tipas').AsString = 'G') then begin //Plintusai
    storis := DS.DataSet.FieldByName('storis').AsFloat;
    plotis := DS.DataSet.FieldByName('plotis').AsFloat;
    ilgis := DS.DataSet.FieldByName('ilgis').AsFloat;
    kiekis := DS.DataSet.FieldByName('kiekis').AsFloat;

    turis := storis*plotis*ilgis/1000000000;
    plotas := ilgis/1000.0;

    DS.DataSet['turis'] := turis;

    eVTuris.Value := turis*kiekis;

    DS.DataSet['plotas'] := plotas;
  end;
end;


procedure TfMedPirkPIrasai.bPadalinysClick(Sender: TObject);
begin
  RegDokTipas[dtDepart].Forma.Paimti(self.PriimtiPadalinys);
end;

procedure TfMedPirkPIrasai.PriimtiPadalinys(Sender: TObject);
begin
  if not (DS.DataSet.State in [dsInsert, dsEdit]) then Exit;
  DS.DataSet['DepID'] := dmBendras.tbDepart['ID'];
  RegDokTipas[dtDepart].Forma.Paimti(nil);
  ActiveControl := dbeFSKodas;
end;

procedure TfMedPirkPIrasai.bFSClick(Sender: TObject);
begin
  RegDokTipas[dtFS].Forma.Paimti(self.PriimtiFS);
end;

procedure TfMedPirkPIrasai.PriimtiFS(Sender: TObject);
begin
  if not (DS.DataSet.State in [dsInsert, dsEdit]) then Exit;
  DS.DataSet['FSID'] := dmBendras.tbFS['ID'];
  RegDokTipas[dtFS].Forma.Paimti(nil);
  ActiveControl := bOK;
end;

procedure TfMedPirkPIrasai.bPadalinysValytiClick(Sender: TObject);
begin
  if not (DS.DataSet.State in [dsInsert, dsEdit]) then Exit;
  DS.DataSet['DepID'] := -1;
end;

procedure TfMedPirkPIrasai.bFSValytiClick(Sender: TObject);
begin
  if not (DS.DataSet.State in [dsInsert, dsEdit]) then Exit;
  DS.DataSet['FSID'] := -1;
end;

end.
